<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>刮刮乐</title>
    <style>
        #can {
            border: 1px solid #ccc;
        }

        .box {
            position: absolute;
            left: 150px;
            top: 50px;
            z-index: -1;
        }
    </style>
</head>

<body>
    <canvas id="can" width="400px" height="100"></canvas>
    <div class="box">恭喜中奖500w</div>
</body>
<script>
    let cans = document.querySelector('#can')
    let context = cans.getContext('2d')

    context.fillStyle = 'darkgray';
    context.fillRect(0, 0, 400, 100);

    context.fillStyle = '#fff';
    context.font = '20px sans-serif';
    context.fillText('刮刮乐', 160, 60);

    let isDraw = false
    cans.onmousedown = function () {
        console.log('按下');
        isDraw = true
    }

    cans.onmousemove = function (e) {
        if (!isDraw) return
        console.log('移动');

        let x = e.pageX - this.offsetLeft
        let y = e.pageY - this.offsetTop

        context.beginPath(); //开始绘画
        context.arc(x, y, 5, Math.PI / 180 * 360, false);
        context.fill(); //填充
        context.closePath(); //结束绘制路径

    }


    cans.onmouseup = function () {
        console.log('抬起');
        isDraw = false
    }
</script>

</html>